package com.mdn.gcalendar.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mdn.gcalendar.service.JsonReaderService;

@Controller
@RequestMapping("/feeds/calendar")
public class CalendarController {

	@Autowired
	JsonReaderService jsonReaderService;
	
	@RequestMapping(value="/timeZone", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getTimeZone() {
		return jsonReaderService.getTemplateContent("calendar/get/timeZone.json", null);
	}
	
	@RequestMapping(value="/timeZone/{calendarId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getTimeZoneCalendar(@PathVariable String calendarId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("calendarId", calendarId);
		return jsonReaderService.getTemplateContent("calendar/get/timeZoneCalendar.json", model);
	}

	@RequestMapping(value="/eventList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getEventList() {
		return jsonReaderService.getTemplateContent("calendar/get/eventList.json", null);
	}
	
	@RequestMapping(value="/aclList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getAclList() {
		return jsonReaderService.getTemplateContent("calendar/get/aclList.json", null);
	}

	@RequestMapping(value="/aclList/{calendarId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getAclListCalendar(@PathVariable String calendarId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("calendarId", calendarId);
		return jsonReaderService.getTemplateContent("calendar/get/aclListCalendar.json", model);
	}

	@RequestMapping(value="/calendarList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getCalendarList() {
		return jsonReaderService.getTemplateContent("calendar/get/calendarList.json", null);
	}

	@RequestMapping(value="/event/move", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getEventMove() {
		return jsonReaderService.getTemplateContent("calendar/get/eventMove.json", null);
	}
	
	@RequestMapping(value="/event/{eventId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String getEvent(@PathVariable String eventId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("eventId", eventId);
		return jsonReaderService.getTemplateContent("calendar/get/event.json", model);
	}

// TODO: En los métodos post, put y delete habrá que ver si necesitamos parsear el json recibido en el request

	@RequestMapping(value="/event/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventCreate(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/eventCreate.json", null);
	}

	@RequestMapping(value="/event/{calendarId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventCalendar(@PathVariable String calendarId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("calendarId", calendarId);
		return jsonReaderService.getTemplateContent("calendar/post/eventCalendar.json", model);
	}

	@RequestMapping(value="/acl/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postAclCreate(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/aclCreate.json", null);
	}

	@RequestMapping(value="/acl/{calendarId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postAclCalendar(@PathVariable String calendarId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("calendarId", calendarId);
		return jsonReaderService.getTemplateContent("calendar/post/aclCalendar.json", model);
	}

	@RequestMapping(value="/event/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventCopy(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/eventCopy.json", null);
	}

	@RequestMapping(value="/event/createQuick", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventCreateQuick(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/eventCreateQuick.json", null);
	}

	@RequestMapping(value="/event/batch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventBatch(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/eventBatch.json", null);
	}

	@RequestMapping(value="/event/batchEvents", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventBatchEvents(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/eventBatchEvents.json", null);
	}

	@RequestMapping(value="/event/batchGetEvent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postEventBatchGetEvent(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/eventBatchGetEvent.json", null);
	}

	@RequestMapping(value="/event/freeBusy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String postFreeBusy(HttpServletRequest request) {
		return jsonReaderService.getTemplateContent("calendar/post/freeBusy.json", null);
	}

	@RequestMapping(value="/event/{eventId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String putEvent(@PathVariable String eventId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("eventId", eventId);
		return jsonReaderService.getTemplateContent("calendar/put/event.json", model);
	}
	
	@RequestMapping(value="/acl/{aclId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String putAcl(@PathVariable String aclId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("aclId", aclId);
		return jsonReaderService.getTemplateContent("calendar/put/acl.json", model);
	}

	@RequestMapping(value="/acl/{aclId}/{calendarId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String putAclCalendar(@PathVariable String aclId, @PathVariable String calendarId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("aclId", aclId);
		model.put("calendarId", calendarId);
		return jsonReaderService.getTemplateContent("calendar/put/aclCalendar.json", model);
	}

	@RequestMapping(value="/event/{eventId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String deleteEvent(@PathVariable String eventId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("eventId", eventId);
		return jsonReaderService.getTemplateContent("calendar/delete/event.json", model);
	}
	
	@RequestMapping(value="/acl/{aclId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String deleteAcl(@PathVariable String aclId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("aclId", aclId);
		return jsonReaderService.getTemplateContent("calendar/delete/acl.json", model);
	}

	@RequestMapping(value="/acl/{aclId}/{calendarId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String deleteAclCalendar(@PathVariable String aclId, @PathVariable String calendarId) {
		Map<String, String> model = new HashMap<String, String>();
		model.put("aclId", aclId);
		model.put("calendarId", calendarId);
		return jsonReaderService.getTemplateContent("calendar/delete/aclCalendar.json", model);
	}
}
